package com.app;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.app.bean.LoginBean;
import com.google.gson.Gson;
import org.json.JSONObject;

public class LoginActivity2 extends AppCompatActivity {

    private EditText userNameEt;
    private EditText passWordEt;
    private Button loginBt;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login2);
        initView();
    }
//    ctrl+alt+f
    private void initView(){
        userNameEt = findViewById(R.id.username_et);
        passWordEt = findViewById(R.id.password_et);
        loginBt = findViewById(R.id.login_bt);
        loginBt = findViewById(R.id.login_bt);
        loginBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               initData();
            }
        });
        TextView tvRegister = findViewById(R.id.tv_regist);
        tvRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity2.this,RegistActivity.class);
                startActivity(intent);
            }
        });

    }

    private void initData(){
        //获取用户输入的用户名和密码
        String username = userNameEt.getText().toString()+"";
        String password = passWordEt.getText().toString()+"";
        //将获取的用户名和密码通过json形式传给服务端
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("name",username);//用户名
            jsonObject.put("password",password);//密码
        } catch (Exception e) {
            e.printStackTrace();
        }
        //创建网络请求队列
        RequestQueue requestQueue = Volley.newRequestQueue(LoginActivity2.this);

        //发起网络请求
        String url = "http://www.xhjjyst.com/freshmenapp/user/login";//发送请求的地址
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
                (Request.Method.POST,//请求方法
                url,//地址
                jsonObject,//获取到的用户名和密码
                new Response.Listener<JSONObject>() {//回调函数
            @Override
            public void onResponse(JSONObject jsonObject) {
                Gson gson = new Gson();
                LoginBean loginBean = gson.fromJson(jsonObject.toString(),LoginBean.class);
                if (loginBean.isSuccess()){
                    //登陆成功跳转到主体界面
                    Intent intent = new Intent(LoginActivity2.this,MainActivity.class);
                    startActivity(intent);
                    Toast.makeText(LoginActivity2.this, "成功", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(LoginActivity2.this,loginBean.getMsg(),Toast.LENGTH_SHORT).show();
                }
              //  Toast.makeText(LoginActivity2.this,jsonObject.toString(),Toast.LENGTH_SHORT).show();

            }
        }, new Response.ErrorListener(){

                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Toast.makeText(LoginActivity2.this, "网络请求失败"+volleyError, Toast.LENGTH_SHORT).show();
                    }
                });
        requestQueue.add(jsonObjectRequest);//将请求内容放入队列中
    }
}
